const {jwtDecode} = require('../util/jwt')

const adminModel = require("../db/model/adminModel")
module.exports = async (req, res, next) => {
  const {
    token
  } = req.headers;
  const jwtDecodeResult = jwtDecode(token, 'overlord')

  if (jwtDecodeResult.code === 0) {
    return res.send({
      code: 20000,
      msg: '检测到，您没有权限访问，请登录拥有权限的账户',
      data: null
    })
  }
//  解码成功，开始判断token正确的用户是否处在登录状态
  try {
    const findResult = await adminModel.findOne({
      adminName: jwtDecodeResult.adminName
    })
    if (findResult.status === 0){
      return res.send({
        code:20000,
        msg:'检测到您为登录账号，请重新登录。',
        data:null
      })
    }
    next();
  }catch (e) {
    console.log('权限控制中出现异常'+e.message)
    res.send({
      code: 20000,
      msg: "权限控制中:查询status状态出现异常" + e.message,
      data: null
    })
  }
}

